package cn.cnhis.online.activity.loading.view;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.widget.Toast;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Map;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
import cn.cnhis.online.BuildConfig;
import cn.cnhis.online.R;
import cn.cnhis.online.activity.loading.contract.ILoadingContract;
import cn.cnhis.online.activity.loading.presenter.LoadingPresenter;
import cn.cnhis.online.activity.login.view.LoginActivity;
import cn.cnhis.online.activity.main.view.MainActivity;
import cn.cnhis.online.base.ActivityStack;
import cn.cnhis.online.base.BaseMvpActivity;
import cn.cnhis.online.mymodule.Constants;
import cn.cnhis.online.mymodule.sp.MySpUtils;

/**
 * @author 刘祥龙
 * @describe
 * @date 2020/3/3 0003  14:14
 * - generate by MvpAutoCodePlus plugin.
 */

public class LoadingActivity extends BaseMvpActivity<ILoadingContract.View, ILoadingContract.Presenter> implements ILoadingContract.View {


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActivityStack.getInstance().pushActivity(this);
        setContentView(R.layout.activity_loading);
        boolean isLogin = MySpUtils.getLoginStatus(this);
        if (BuildConfig.DEBUG) Log.d("LoadingActivity", "isLogin:" + isLogin);
//        getPresenter().getAppActiveMqInfo();




        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                if (isLogin) {
                    startActivity(new Intent(LoadingActivity.this, MainActivity.class));
                } else {
                    startActivity(new Intent(LoadingActivity.this, LoginActivity.class));
                }
                finish();
            }
        }, 3000);

    }


    @Override
    public void showErr(String e) {

    }

    @Override
    public ILoadingContract.Presenter createPresenter() {
        return new LoadingPresenter();
    }

    @Override
    public ILoadingContract.View createView() {
        return this;
    }

    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
    @Override
    public void getAppActiveMqInfo(Map map) {
        if (BuildConfig.DEBUG) Log.d("LoadingActivity", "getAppActiveMqInfo:" + map);
        JSONObject jsonObject = toJSONObject(map);
        try {
            if (jsonObject.getString(Constants.RESULT).equals(Constants.SUCCESS)) {
                String json = jsonObject.getString(Constants.MAP);
                if (!isDestroyed()) {
                    MySpUtils.setMQINFO(this, json);
                }

            } else if (jsonObject.getString(Constants.RESULT).equals(Constants.Failure)) {
                Toast.makeText(this, "获取MQ信息失败！", Toast.LENGTH_SHORT).show();
            } else if (jsonObject.getString(Constants.RESULT).equals(Constants.NO_LOGIN)) {
                Toast.makeText(this, "免密校验失败！", Toast.LENGTH_SHORT).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}

